package com.lyf.scm.admin.domain.remote.category.facade;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.admin.domain.remote.category.CategoryRemote;
import com.lyf.scm.admin.domain.remote.category.dto.*;
import com.lyf.scm.common.constants.CommonConstants;
import com.lyf.scm.common.enums.ResponseMsg;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.core.exception.RomeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 品类中心
 *
 * @author wuzhuang
 * @date 2020/08/15
 */
@Slf4j
@Service
public class CategoryFacade {

    @Resource
    private CategoryRemote categoryRemote;

    public List<SkuPurchasePermitDTO> querySkuPermit(List<String> skuCodeList) {
        log.info("查询商品采购权，参数：{}", JSON.toJSONString(skuCodeList));
        Response<List<SkuPurchasePermitDTO>> response = categoryRemote.querySkuPurchasePermit(skuCodeList);
        log.info("查询商品采购权，响应参数：{}", JSON.toJSONString(response));
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(ResponseMsg.EXCEPTION_2003.getCode(), ResponseMsg.EXCEPTION_2003.getMsg());
        }
        List<SkuPurchasePermitDTO> data = response.getData();
        if (CollectionUtils.isEmpty(data)) {
            throw new RomeException(ResponseMsg.EXCEPTION_2004.getCode(), ResponseMsg.EXCEPTION_2004.getMsg());
        }
        return data;
    }

    public List<StorePackageSkuDTO> queryStorePackageSku() {
        Response<PageInfo<StorePackageSkuDTO>> response = categoryRemote.queryStorePackageSku(CommonConstants.PAGE_NUM, CommonConstants.PAGE_SIZE);
        log.info("查询新开店套餐，响应参数：{}", JSON.toJSONString(response));
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(ResponseMsg.EXCEPTION_2003.getCode(), ResponseMsg.EXCEPTION_2003.getMsg() + "获取新开店套餐");
        }
        PageInfo<StorePackageSkuDTO> pageList = response.getData();
        List<StorePackageSkuDTO> skuList = pageList.getList();
        //每页只能取100条
        if (pageList.getTotal() > CommonConstants.PAGE_SIZE) {
            for (int i = 0; i < pageList.getTotal() / CommonConstants.PAGE_SIZE + 1; i++) {
                skuList.addAll(categoryRemote.queryStorePackageSku(CommonConstants.PAGE_NUM + i + 1, CommonConstants.PAGE_SIZE).getData().getList());
            }
        }
        return skuList;
    }

    public void queryWidthSku(String channelCode, String provinceCode, String cityCode, String storeCode, List<String> skuCodes) {
        log.info("查询渠道商品叫货权，渠道、省份、城市、门店、商品编码参数：{}", JSON.toJSONString(channelCode), JSON.toJSONString(provinceCode), JSON.toJSONString(cityCode), JSON.toJSONString(skuCodes));
        Response response = categoryRemote.queryWidthSku(channelCode, provinceCode, cityCode, storeCode, skuCodes);
        log.info("查询渠道商品叫货权，响应参数：{}", JSON.toJSONString(response));
        if (!ResponseMsg.SUCCESS.getCode().equals(response.getCode())) {
            throw new RomeException(ResponseMsg.EXCEPTION_2003.getCode(), ResponseMsg.EXCEPTION_2003.getMsg() + response.getMsg()
                    + "，获取渠道商品叫货权失败，渠道、省份、城市、门店、商品编码为(" + channelCode + "、" + provinceCode + "、" + cityCode + "、" + storeCode + "、" + skuCodes + ")");
        }
        //处理传过来的数据
        String[] strs = response.getData().toString().replace("[","").replace("]","").split(", ");
        List<String> list = new ArrayList<>();
        for (String str: strs) {
            list.add(str);
        }
        List<String> skuCodeList = new ArrayList<>();
        //只会少不会多
        if (list.size() < skuCodes.size()) {
            skuCodeList.addAll(skuCodes.stream().filter(item -> !list.contains(item)).collect(Collectors.toList()));
            throw new RomeException(ResponseMsg.PARAM_ERROR_2007.getCode(), ResponseMsg.PARAM_ERROR_2007.getMsg() + "，错误编码为：" + skuCodeList);
        }
    }
}
